package com.scnu.edu.dto;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.scnu.edu.entity.*;
import com.scnu.edu.mapper.*;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * 电影数据传输对象
 */
@Data
@Component
public class MovieDTO {
    private Long id;
    private String name;
    private String coverUrl;
    private String playUrl;
    private String description;
    private Double score;
    private Integer playCount;
    private Boolean isVip;
    private List<Long> categoryIds;
    private List<String> categories;
    private Date releaseDate;
    private List<Director> directors;
    private List<Actor> actors;

    @Autowired
    private MovieMapper movieMapper;
    @Autowired
    private MovieDirectorMapper movieDirectorMapper;
    @Autowired
    private MovieActorMapper movieActorMapper;
    @Autowired
    private MovieCategoryRelationMapper movieCategoryRelationMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    public MovieDTO() {}

    public MovieDTO(Long movieId) {
        Movie movie = movieMapper.selectById(movieId);
        if (movie == null) {
            throw new IllegalArgumentException("Movie not found with id: " + movieId);
        }

        this.id = movie.getId();
        this.name = movie.getName();
        this.coverUrl =movie.getCoverUrl();
        this.playUrl = movie.getPlayUrl();
        this.description = movie.getDescription();
        this.score = movie.getScore();
        this.playCount = movie.getPlayCount();
        this.isVip = movie.getIsVip();
        this.releaseDate = movie.getReleaseDate();

        this.directors = movieDirectorMapper.findDirectorsByMovieId(movieId);
        this.actors = movieActorMapper.findActorsByMovieId(movieId);

        this.categoryIds = movieCategoryRelationMapper.findCategoryIdsByMovieId(movieId);
        this.categories = categoryMapper.findNamesByIds(this.categoryIds);
    }

    public MovieDTO(Movie movie) {
        if (movie != null) {
            this.id = movie.getId();
            this.name = movie.getName();
            this.coverUrl = movie.getCoverUrl();
            this.description = movie.getDescription();
            this.score = movie.getScore();
            this.playCount = movie.getPlayCount();
            this.isVip = movie.getIsVip();
            this.releaseDate = movie.getReleaseDate();
            this.playUrl = movie.getPlayUrl();
        }
    }

    public void setDirectors(List<Director> directors) {
        this.directors = directors;
    }

    public void setActors(List<Actor> actors) {
        this.actors = actors;
    }


    public void setMovie(Movie movie) {
    }
}
